Imports System.Data
Imports System.Data.SqlClient
Imports DevExpress.XtraEditors.Controls
Imports System.Threading
Imports System.Globalization

Public Class FromPeopleList

    Public EmailOrSMS As String

    Private Sub LoadCategory()
        'Load Category
        Dim str As String = "SELECT * FROM Category_People"
        Dim cmd As New SqlCommand(str, gCnnPCE)
        Dim da As New SqlDataAdapter(cmd)
        Dim ds As New DataSet
        Me.cboCategory.Properties.Columns.Clear()
        cboCategory.Properties.DataSource = Nothing
        da.Fill(ds, "Category")

        cboCategory.Properties.DataSource = ds.Tables("Category")
        cboCategory.Properties.DisplayMember = "CategoryName"
        cboCategory.Properties.ValueMember = "CategoryID"

        Dim CollCategory As DevExpress.XtraEditors.Controls.LookUpColumnInfoCollection = cboCategory.Properties.Columns
        CollCategory.Add(New LookUpColumnInfo("CategoryName", "Category", 0))
        CollCategory.Add(New LookUpColumnInfo("CategoryID", "CategoryID", 0, DevExpress.Utils.FormatType.None, 0, False, DevExpress.Utils.HorzAlignment.Center))

    End Sub

    Private Sub FromPeopleList_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Load Category
        LoadCategory()

        RefreshGrid(0)

        'Dim str As String = "SELECT Name, MobilePhone, Name + N' ' + MobilePhone AS NameAndMobile FROM People"
        'Dim cmd As New SqlCommand(str, gCnnPCE)
        'Dim ds As New DataSet
        'Dim da As New SqlDataAdapter(cmd)
        'da.Fill(ds, "PeopleMobileNo")
        'Me.listPeople.DataSource = ds.Tables("PeopleMobileNo")
        'Me.listPeople.DisplayMember = "NameAndMobile"
        'Me.listPeople.ValueMember = "MobilePhone"

        'Dim CollCategory As DevExpress.XtraEditors.ListBoxControl.MultiColumnMo
        'CollCategory.Add(New LookUpColumnInfo("CategoryName", "Category", 0))
        'CollCategory.Add(New LookUpColumnInfo("CategoryID", "CustomerID", 0, DevExpress.Utils.FormatType.None, 0, False, DevExpress.Utils.HorzAlignment.Center))

    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub btnChoose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnChoose.Click
        Dim i As Integer
        For i = 0 To Me.listPeople.ItemCount - 1
            If listPeople.GetItemCheckState(i) = CheckState.Checked Then
                PeopleMobileNo = PeopleMobileNo & ";" & listPeople.GetItemValue(i)
            End If
        Next

        Me.Close()
    End Sub

    Private Sub cboCategory_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboCategory.EditValueChanged
        RefreshGrid(Me.cboCategory.EditValue)
    End Sub

    Private Sub RefreshGrid(ByVal CategoryID As Integer)
        Dim str As String

        Select Case EmailOrSMS
            Case "EMAIL"
                If CategoryID = 0 Then
                    str = "SELECT Name, Email, Name + N' ' + Email AS NameAndContact FROM People WHERE Active = 1"
                Else
                    str = "SELECT Name, Email, Name + N' ' + Email AS NameAndContact FROM People WHERE Active = 1 AND Category = " & CategoryID
                End If

            Case "SMS"
                If CategoryID = 0 Then
                    str = "SELECT Name, MobilePhone, Name + N' ' + MobilePhone AS NameAndContact FROM People WHERE MobilePhone <> '' AND NOT MobilePhone IS NULL AND Active = 1"
                Else
                    str = "SELECT Name, MobilePhone, Name + N' ' + MobilePhone AS NameAndContact FROM People WHERE MobilePhone <> '' AND Active = 1 AND NOT MobilePhone IS NULL AND Category = " & CategoryID
                End If

        End Select


        Dim cmd As New SqlCommand(str, gCnnPCE)
        Dim ds As New DataSet
        Dim da As New SqlDataAdapter(cmd)
        da.Fill(ds, "PeopleMobileNo")
        Me.listPeople.DataSource = ds.Tables("PeopleMobileNo")
        Me.listPeople.DisplayMember = "NameAndContact"
        Select Case EmailOrSMS
            Case "EMAIL"
                Me.listPeople.ValueMember = "Email"
            Case "SMS"
                Me.listPeople.ValueMember = "MobilePhone"
        End Select

    End Sub

    Private Sub chkSelectAll_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkSelectAll.CheckedChanged
        If Me.chkSelectAll.Checked = True Then
            Dim i As Integer
            For i = 0 To listPeople.ItemCount - 1
                listPeople.SetItemChecked(i, True)
            Next
        End If
    End Sub

    Public Sub New()

        If LanguageType <> "Default" Then
            Thread.CurrentThread.CurrentUICulture = New CultureInfo(LanguageType)
        End If
        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

    End Sub
End Class